import pytest
from unittest.mock import MagicMock


# 模拟API客户端
class CvmClient:
    def __init__(self):
        self.responses = {
            "Success": {"Response": {"RequestId": "mock-req-123"}},
            "InvalidParameter": {"Response": {"Error": {"Code": "InvalidParameter"}}},
            "InvalidInstanceId": {"Response": {"Error": {"Code": "InvalidInstanceId"}}},
            "InstanceNotRunning": {"Response": {"Error": {"Code": "InstanceNotRunning"}}},
            "AuthFailure": {"Response": {"Error": {"Code": "AuthFailure"}}},
            "LimitExceeded": {"Response": {"Error": {"Code": "LimitExceeded"}}},
            "InternalError": {"Response": {"Error": {"Code": "InternalError"}}}
        }

    def reboot_instances(self, instance_ids):
        # 优先处理错误注入
        if isinstance(instance_ids, dict) and "error_code" in instance_ids:
            return self.responses.get(instance_ids["error_code"], self.responses["InternalError"])

        # 参数校验逻辑
        if not isinstance(instance_ids, list) or len(instance_ids) == 0:
            return self.responses["InvalidParameter"]
        for instance_id in instance_ids:
            if not isinstance(instance_id, str) or len(instance_id.strip()) == 0:
                return self.responses["InvalidParameter"]

        return self.responses["Success"]


# 测试用例
class TestRebootInstances:
    @pytest.fixture
    def client(self):
        return CvmClient()

    # 正常用例
    def test_success(self, client):
        response = client.reboot_instances(["ins-123", "ins-456"])
        assert "RequestId" in response["Response"]

    # 参数验证类错误
    @pytest.mark.parametrize("invalid_input", [
        [],  # 空列表
        ["ins-123", ""],  # 包含空字符串
        ["ins-123", 123],  # 类型错误
        {"invalid_key": "ins-123"}  # 错误参数格式
    ])
    def test_invalid_params(self, client, invalid_input):
        response = client.reboot_instances(invalid_input)
        assert response["Response"]["Error"]["Code"] == "InvalidParameter"

    # 资源状态类错误
    def test_instance_not_running(self, client):
        response = client.reboot_instances({"error_code": "InstanceNotRunning"})
        assert response["Response"]["Error"]["Code"] == "InstanceNotRunning"

    # 权限类错误
    def test_auth_failure(self, client):
        response = client.reboot_instances({"error_code": "AuthFailure"})
        assert response["Response"]["Error"]["Code"] == "AuthFailure"

    # 业务限制类错误
    def test_limit_exceeded(self, client):
        response = client.reboot_instances({"error_code": "LimitExceeded"})
        assert response["Response"]["Error"]["Code"] == "LimitExceeded"

    # 系统内部错误
    def test_internal_error(self, client):
        response = client.reboot_instances({"error_code": "InternalError"})
        assert response["Response"]["Error"]["Code"] == "InternalError"

    # 特殊场景测试
    def test_empty_instance_list(self, client):
        response = client.reboot_instances([])
        assert response["Response"]["Error"]["Code"] == "InvalidParameter"

    def test_nonexistent_instance(self, client):
        response = client.reboot_instances(["ins-invalid"])
        assert response["Response"]["Error"]["Code"] == "InvalidInstanceId"

    # 参数校验增强测试
    def test_invalid_instance_type(self, client):
        response = client.reboot_instances([None, "ins-123"])
        assert response["Response"]["Error"]["Code"] == "InvalidParameter"


# 执行测试
if __name__ == "__main__":
    pytest.main(["-v", "--color=yes"])